package com.thefirst.stage.homework2;

/*
题目：编写程序，循环接收用户从键盘输入多个字符串，
直到输入“end”时循环结束，
并将所有已输入的字符串按字典顺序倒序录入到项目下abc.txt中

作者：翁海龙
 */
import java.io.*;
import java.sql.SQLOutput;
import java.util.Arrays;

public class Homework2 {

    public static void main(String[] args) {

        BufferedReader br= null;
        PrintStream ps= null;
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            ps = new PrintStream(new FileOutputStream("d:/abc.txt"));

            while(true){
                System.out.println("请输入字符：");
                String str=br.readLine();
                char[]ch=str.toCharArray();
                Arrays.sort(ch);

                //printSort(ch);

                if("end".equals(str)){
                    System.out.println("循环结束！");
                    break;
                }
                else{
                    System.out.println("顺序录入：");
                    for(int i=0;i<=ch.length-1;i++){
                        System.out.println("已输入的字符串:"+ch[i]);
                        ps.println(ch[i]);
                    }
                    System.out.println();
                    System.out.println();

                    System.out.println("倒序录入：");
                    for (int i=0;i<=ch.length-1;i++){
                        System.out.println("已输入的字符串:"+ch[ch.length-i-1]);
                        ps.println(ch[ch.length-i-1]);
                    }


                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null !=ps){
                ps.close();
            }
           if(null !=br){
               try {
                   br.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }

        }

    }
   /*
    private static void printSort(char[] ch){

        System.out.println("顺序录入：");
        for(int i=0;i<=ch.length-1;i++){
            System.out.println("已输入的字符串:"+ch[i]);
        }
        System.out.println();
        System.out.println();

        System.out.println("倒序录入：");
        for (int i=0;i<=ch.length-1;i++){
            System.out.println("已输入的字符串:"+ch[ch.length-i-1]);
        }
    }*/


}
